/************************************************************
* @file   gkt_bit.h
************************************************************/

#ifndef GKT_BIT_H
#define GKT_BIT_H

#include "gkt_types.h"
#include "gkt_core.h"

#ifdef __cplusplus
extern "C" {
#endif

extern const uint8_t __gc_u8_reverse_half_table[];

#define gkt_bit_reverse_u8(__data_u8, __result_u8)	\
	do {	\
		__result_u8 = __gc_u8_reverse_half_table[((uint8_t)(__data_u8)) & 0x0f]	\
		| (__gc_u8_reverse_half_table[((uint8_t)(__data_u8)) >> 4] >> 4);	\
	} while (0)

#define gkt_bit_reverse_u16(__data_u16, __result_u16)	\
	do {	\
		uint8_t __result_u16_lo, __result_u16_hi;	\
		gkt_bit_reverse_u8((uint8_t)(__data_u16), __result_u16_hi);	\
		gkt_bit_reverse_u8((uint8_t)((__data_u16) >> 8), __result_u16_lo);	\
		__result_u16 = ((uint16_t)__result_u16_hi << 8) | __result_u16_lo;	\
	} while (0)

#if defined(GKT_SYSINC_HAS_RBIT) && GKT_SYSINC_HAS_RBIT
#define gkt_bit_reverse_u32(__data_u32, __result_u32)	\
	do { __result_u32 = __RBIT(__data_u32); } while (0)
#else
#define gkt_bit_reverse_u32(__data_u32, __result_u32)	\
	do {	\
		uint16_t __result_u32_lo, __result_u32_hi;	\
		gkt_bit_reverse_u16((uint16_t)(__data_u32), __result_u32_hi);	\
		gkt_bit_reverse_u16((uint16_t)((__data_u32) >> 16), __result_u32_lo);	\
		__result_u32 = ((uint32_t)__result_u32_hi << 16) | __result_u32_lo;	\
	} while (0)
#endif

#ifdef __cplusplus
}
#endif

#endif	/* GKT_BIT_H */

